Skip to content

Add ToArrayAsync and ToImmutableArrayAsync on a DbDataReader - #285

Merged
electricessence merged 1 commit into
masterfrom
feature/reader-toarray-async
Dec 2, 2026
Merged

electricessence merged 1 commit into
masterfrom
feature/reader-toarray-async

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

Adds ToArrayAsync and ToImmutableArrayAsync on a DbDataReader, beside the transform form of ToListAsync.

Each reads the remaining rows of the current result set through an asynchronous transform into pooled chunks and copies them once, into an array of exactly their number. On .NET 8 and later the immutable array wraps that array and is not copied again; on .NET Standard it is a copy (there is no way to wrap an array there).

The three share one private core, specialized per result by a struct type argument (the build the mapped terminals already use), so none has a closure. ToListAsync keeps its behavior, including the argument guards inside the returned task. The two new members are in PublicAPI.Unshipped.txt and in the release notes under "Changes since rc.16".

Bytes per call against a builder of unknown capacity

GC.GetAllocatedBytesForCurrentThread around one call, warmed first, the smallest of 15 runs, in Release, over an in-memory DataTableReader (its ReadAsync finishes at once and allocates nothing). "Net" is the bytes less those of reading the rows alone. No timing.

Rows ToImmutableArrayAsync (net) ImmutableArray.CreateBuilder<int>() + ToImmutable() (net) ToArrayAsync (net) List<int> + ToArray() (net)
0 0 88 -24 8
1 32 120 8 80
1,000 4,048 12,432 4,024 12,448
100,000 400,048 1,448,984 400,024 1,449,000

The builder never wins. At 100,000 rows the chunk path allocates exactly the result (4 bytes a row); the builder allocates its growth as well as the final copy. The explicit probe ReaderCollectAllocationProbe.Probe_BytesPerCall prints the table; a Release-only test pins that the chunk path allocates less than a builder at 1,000 and 100,000 rows.

Tests

  • Every row, in order, in an array of exactly that length (none, one, a few, 2,000) for both new methods; the immutable array's backing array has that length too.
  • The null guards for all three methods: ArgumentNullException with the right parameter name, thrown inside the returned task (the calls are made outside the assert, so a throw at the call would fail the test).
  • A transform that throws, and a cancel through a reader whose ReadAsync honors the token, for all three methods: the task faults or is cancelled, and the chunk chain is parked again (read through the one spare slot of a row type no other test uses).

Checks

  • Core tests: 15587 pass in Release with the generator on; 15568 pass (16 skipped) in Debug with it off.
  • 231 sample checks pass; the Visual Studio solution build has no errors; dotnet format style and analyzers at --severity info report nothing on all 14 projects; the docs build is clean.
  • Reviewed by three Haiku reviewers (correctness and leaks, API and docs, test adequacy), arbitrated. Real findings fixed: the no-copy claim was unconditional (now .NET 8 and later only), and the guard test could not tell a throw at the call from a throw in the task (now it can). Claims not borne out by the code or the build were dropped (a release-note heading that exists; a lambda conversion that compiles).

🤖 Generated with Claude Code

Siblings of the transform form of `ToListAsync` on `DbDataReader`: each reads the remaining rows of the current result set through an asynchronous transform into pooled chunks and copies them once, into an array of exactly their number. On .NET 8 and later the immutable array wraps that array and is not copied again; on .NET Standard it is a copy.

The three share one private core, specialized per result by a struct type argument (the build the mapped terminals use), so none has a closure. `ToListAsync` keeps its behavior, including the guards inside the returned task.

Tests: every row in order and the exact length (none, one, a few, thousands), the null guards (in the returned task), a transform that throws and a cancel (the chain is parked again after each), for the three terminals; and bytes per call against an `ImmutableArray` builder of unknown capacity, with an explicit probe that prints the numbers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit b9b2f50 into master Dec 2, 2026
9 checks passed
@electricessence
electricessence deleted the feature/reader-toarray-async branch December 2, 2026 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant